﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Net.NetworkInformation;
using SampleResizebleItem.Controls;
using SampleResizebleItem.Models;
using SampleResizebleItem.Services;

namespace SampleResizebleItem.ViewModels
{
    public class MainViewModel : Microsoft.Practices.Prism.ViewModel.NotificationObject
    {
        private Services.Service _service;
        public System.Collections.ObjectModel.ObservableCollection<Models.ModelImages> Photos { get; set; }
        public MainViewModel()
        {
            _service = new Service();
            Photos = new ObservableCollection<ModelImages>();
        }

        public void LoadFlickr()
        {
            if (DeviceNetworkInformation.IsNetworkAvailable)
            {
                _service.LoadFlickr((items, exception) =>
                {
                    if (exception != null)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show("Error : " + exception.Message);
                            RaisePropertyChanged("");

                        });

                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            int counter = 0;

                            foreach (Models.ModelImages item in items)
                            {
                                if(counter == 0)
                                {
                                    if (!Photos.Contains(item))
                                    {
                                        item.RowSpan = 2;
                                        item.ColumnSpan = 2;
                                        Photos.Add(item);
                                    }
                                }
                                else
                                {
                                    if (!Photos.Contains(item))
                                    {
                                        Photos.Add(item);
                                    }
                                }

                                if (counter == 4)
                                    counter = 0;
                                else
                                    counter++;
                                

                            }

                            Combine();

                            RaisePropertyChanged("Photos");
                        });

                    }


                }, "");
            }
        }

        private void Combine()
        {
            App.EventAggregator.GetEvent<Events.EventGetFlickr>().Publish(Photos.ToList());
           
        }
    }
}
